package test.enba.es.controller;

import com.enba.boot.es.EsHelper;
import java.io.IOException;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/es")
public class EsController {

  private final EsHelper esService;

  public EsController(EsHelper esService) {
    this.esService = esService;
  }

  /** 插入数据 */
  @PostMapping("/insert/{index}/{id}")
  public IndexResponse insert(
      @PathVariable String index, @PathVariable String id, @RequestBody String json)
      throws IOException {
    return esService.insert(index, id, json);
  }

  /** 查询数据 */
  @GetMapping("/get/{index}/{id}")
  public GetResponse get(@PathVariable String index, @PathVariable String id) throws IOException {
    return esService.get(index, id);
  }

  /** 更新数据 */
  @PutMapping("/update/{index}/{id}")
  public UpdateResponse update(
      @PathVariable String index, @PathVariable String id, @RequestBody String json)
      throws IOException {
    return esService.update(index, id, json);
  }

  /** 删除数据 */
  @DeleteMapping("/delete/{index}/{id}")
  public DeleteResponse delete(@PathVariable String index, @PathVariable String id)
      throws IOException {
    return esService.delete(index, id);
  }
}
